package stu.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import stu.aistar.model.pojo.Result;
import stu.aistar.model.pojo.User;
import stu.aistar.service.UserService;

import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

//    @GetMapping("/login")
//    public String login(){
//        return "admin/page/user/login";
//    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request){
//          User user1 = userService.login1(user.getPhone(), user.getPassword(),user.getPower())

      User user1 = userService.login1(request.getParameter("phone"), request.getParameter("password"));
        Result result= null;
        if (null == user1){
            result = new Result("404","手机号或密码错误",null);
        }else{
            request.getSession().setAttribute("user1",user1);
//            System.out.println(request.getSession().getAttribute("user"));
            redisTemplate.opsForValue().set("user_"+request.getParameter("phone"),String.valueOf(user1));
            result = new Result("200","登录成功",null);
        }

        return result;
    }
}
